apiready = function() {
seajs.use(['../../V',"../../api","../../jquery","../../app","../../service","../../pop"],function(V,$api,$,$app,service,$pop){
	var vm = {
		el: '.container',
	  	data: {
	  		address_id: '',
	  		name: '',
	  		phone: '',
	  		province_id: '',
	  		city_id: '',
	  		district_id: '',
	  		address: '',
	  		postcode: '',
	  		status: '',
	  		cityTxt: ''
	  	},
	  	methods:{
	  		init: function(){
	  			this.getList();
	  		},
	  		getList: function(){
	  			var unionid = $api.getStorage("shop_unionid"), self = this;
		  		$app.ajax(service.addressGetOne,{
					data: {
						values: {
							unionid: unionid,
							address_id: self.address_id
						}
					}
				},function (res) {
					if(res.code == 0){
						var data = res.data;
						self.address_id = data.address_id;
						self.name =  data.name;
				  		self.phone = data.phone;
				  		self.province_id = data.province_id;
				  		self.city_id = data.city_id;
				  		self.district_id = data.district_id;
				  		self.address = data.address;
				  		self.postcode = data.postcode;
				  		self.status = data.status;
				    	self.cityTxt = data.province +" "+data.city+" "+data.district;
					}else{
						alert(res.message || res.msg);
					}
				});
	  		},
	  		//删除收货地址
	  		addressDel: function(et){
	  			var unionid = $api.getStorage("shop_unionid");
				var self = this;
				$app.ajax(service.addressDel,{
		  			data: {
						values: {
							unionid: unionid,
							address_id: self.address_id
						}
					}
		  		},function(res){
		  			if(res.code == 0){
		  				$pop.msg({
		  					msg: "删除成功",
		  					btns: ["返回","添加新地址"]
		  				},function(data){
		  					if(data.index == 0){
		  						api.sendEvent({
		  							name:'addressAdd_'
		  						})
		  						$api.setTimeout(function(){
		  							api.closeWin();
		  						},300);
		  					}else{
		  						//--添加新地址
		  						$app.openWin('buyAddressAdd');
		  					}
		  				});
		  			}else{
		  				alert(res.message || res.msg);
		  			}
		  		});
	  		},
	  		//--打开城市选择
	  		addressAddCitySelect: function(et){
				var self = this;
				$app.openCitySelect();
				$app.getCitySelect(function(res){
					if(res.code == 0){
						self.province_id = res.data.province_id;
						self.city_id = res.data.city_id;
						self.district_id = res.data.district_id;
						self.cityTxt = res.data.full_name;
					}
				});
			},
	  		//--提交
	  		addressAddSubmit: function(et){
	  			var unionid = $api.getStorage("shop_unionid"), self = this;
	  			this.name = this.$refs.name.value || "";
	  			this.phone = this.$refs.phone.value.replace(/\s+/,"") || "";
	  			this.address = this.$refs.address.value || "";
	  			this.postcode = "";
	  			this.status = this.$refs.status.checked ? 1 : 0;
	  			if(this.name == "" || this.phone == "" || this.province_id == "" || this.city_id == "" || this.district_id == "" || this.address == ""){
	  				$app.toast('请填写完整地址...');
					return;
	  			}
	  			if(this.name.toString().trim().length > 15){
	  				$app.toast('名字最长15个字...');
	  				return;
	  			}
	  			if(this.address.toString().trim.length > 60){
	  				$app.toast('详细地址最长60字...');
	  				return;
	  			}
	  			if(!/^1[3456789][0-9]{9}$/.test(this.phone) && !/^0\d{2,3}-?\d{7,8}$/.test(this.phone)){
  					$app.toast('请输入正确的电话号码...');
  					return;
	  			}
  				$app.ajax(service.addressEdit,{
		  			data: {
						values: {
							unionid: unionid,
							address_id: self.address_id,
							name: self.name,
							phone: self.phone,
							province_id: self.province_id,
							city_id: self.city_id,
							district_id: self.district_id,
							address: self.address,
							postcode: self.postcode,
							status: self.status
						}
					}
		  		},function(res){
	  				if(res.code == 0){
		  				$pop.msg({
		  					msg: "地址修改成功",
		  					btns: ["返回","添加新地址"]
		  				},function(et){
		  					if(et.index == 0){
		  						api.sendEvent({
		  							name:'addressAdd_'
		  						})
		  						$api.setTimeout(function(){
		  							api.closeWin();
		  						},300);
		  					}else{
		  						//--添加新地址
		  						$app.openWin('buyAddressAdd');
		  					}
		  				})
		  			}else{
		  				alert(res.msg || res.message);
		  			}
		  		});
	  		}
	  	},
	  	mounted:function() {
	  		function btnKeyBack(){
	  			api.sendEvent({
					name:'addressAdd_'
				})
				$api.setTimeout(function(){
					api.closeWin();
				},300);
	  		}
  			//--点击上面按钮返回
			$api.addEvt($api.dom('#btn-back-new'), 'click', function() {
	            btnKeyBack();
	        });

			//--返回键
			api.addEventListener({
				name: 'keyback'
			},function(ret, err){
				btnKeyBack();
			});

			//--右滑
	        api.addEventListener({
			    name:'swiperight'
			},function(ret,err){
			    btnKeyBack();
         	});
	  	},
	  	created: function(){
	  		this.address_id = api.pageParam.address_id;
			this.init();
	  	}
	};
	var even = function() {}
	var newVue = new Vue(vm);
	even();
	});
	}
